package com.nui.multiphotopicker.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import xf.tools.Other;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaScannerConnection;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.base.BaseActivity;
import com.nui.multiphotopicker.adapter.ImageBucketAdapter;
import com.nui.multiphotopicker.model.ImageBucket;
import com.nui.multiphotopicker.util.CustomConstants;
import com.nui.multiphotopicker.util.ImageFetcher;
import com.nui.multiphotopicker.util.IntentConstants;

/**
 * 
 * <p>
 * <b>Title：</b> 选择相册
 * </p>
 * <p>
 * <b>Description：</b> 公用的图片选择进入界面
 * </p>
 * 
 * @author tangxin
 * @date 2016年12月6日 下午3:43:44
 */
public class ImageBucketChooseActivity extends BaseActivity {
	private ImageFetcher mHelper;
	private List<ImageBucket> mDataList = new ArrayList<ImageBucket>();
	private ListView mListView;
	private ImageBucketAdapter mAdapter;
	private int availableSize;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		BaseActivity.activity = this;
		setContentView(R.layout.act_image_bucket_choose);
		mHelper = ImageFetcher.getInstance(getApplicationContext());
		initDate();
		initView();
	}

	private void initDate() {
		MediaScannerConnection.scanFile(this, new String[] { "/sdcard/DCIM/camera/*" }, null, null);
		MediaScannerConnection.scanFile(this, new String[] { "/stystm/DCIM/camera/*" }, null, null);
		mDataList = mHelper.getImagesBucketList(false);
		availableSize = getIntent().getIntExtra(IntentConstants.EXTRA_CAN_ADD_IMAGE_SIZE,
				CustomConstants.MAX_IMAGE_SIZE);
	}

	protected void onResume() {
		BaseActivity.activity = this;
		IntentFilter filter = new IntentFilter(Other.VGOPLAY_1);
		registerReceiver(mBroadcastReceiver, filter);
		super.onResume();
	};

	private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(Other.VGOPLAY_1)) {
				finish();
			}
		}
	};

	protected void onDestroy() {
		unregisterReceiver(mBroadcastReceiver);
		super.onDestroy();
	};

	private void initView() {
		mListView = (ListView) findViewById(R.id.listview);
		mAdapter = new ImageBucketAdapter(this, mDataList);
		mListView.setAdapter(mAdapter);
		TextView titleTv = (TextView) findViewById(R.id.title);
		titleTv.setText("相册");

		SetRelativiLayoutPadding(R.id.main_head_top_rela_header);

		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				selectOne(position);

				Intent intent = new Intent(ImageBucketChooseActivity.this, ImageChooseActivity.class);
				intent.putExtra(IntentConstants.EXTRA_IMAGE_LIST, (Serializable) mDataList.get(position).imageList);
				intent.putExtra(IntentConstants.EXTRA_BUCKET_NAME, mDataList.get(position).bucketName);
				intent.putExtra(IntentConstants.EXTRA_CAN_ADD_IMAGE_SIZE, availableSize);
				startActivity(intent);
				if ("1".equals(EmmApplication.getData("Picture"))) {

				} else {
					finish();
				}

			}
		});

		TextView cancelTv = (TextView) findViewById(R.id.action);// 返回按钮
		cancelTv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if ("1".equals(EmmApplication.getData("Picture"))) {
					finish();
				} else {
					finish();
				}
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// if ("1".equals(getParam("Picture", "0"))) {
			finish();
			// } else {
			// Intent intent = new Intent(ImageBucketChooseActivity.this,
			// AtLineActivity.class);
			// startActivity(intent);
			// finish();
			// }
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * <p>
	 * Title:让选中的相册的selected的值置为true(暂时没有用到)
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param position
	 * 
	 * @author zhaofei
	 * 
	 * @date 2017年1月11日 下午3:46:01
	 */
	private void selectOne(int position) {
		int size = mDataList.size();
		for (int i = 0; i != size; i++) {
			if (i == position)
				mDataList.get(i).selected = true;
			else {
				mDataList.get(i).selected = false;
			}
		}
		mAdapter.notifyDataSetChanged();
	}

}
